/**
 * 
 */
package com.bitBusy.manageyourriches.emailManagement;

import java.io.File;

import android.util.Log;

/**
 * @author SumaHarsha
 *
 */
public class Postman {
	/** email sent*/
	public static final String EMAILSENT = "Data exported successfully";
	
	/** email sent*/
	public static final String SYSTEMFAIL = "System fail: data not exported";

	/** email error*/
	public static final String EMAILERROR = "email address is not in the correct format";

	public static final String NOTSET = " Export not scheduled - is there already an export scheduled for the same date and email?";

	private static final String EMAILSUBJECT = "Expense report";
	private static final String EMAILBODY = "Please find attached the expense report, with details of your expenditure till date.\n\n\n\n\n\n" +
			"Please do not reply to this message - the inbox is unmonitored. In case of any queries/feedback, please write to " +
			"bitBusyDroid@gmail.com. Thank you for using Manage your riche$!";
	private static final String SENDINGERROR = "Error sending email.";
	
	public enum EMAILSENTCODES
	{
		SUCCESS,
		FAILUREEMAILADDRESS,
		FAILURESYSEM;
	}
	/**
	 * @param email
	 */
	public static EMAILSENTCODES send(String the_email, File the_attachment) {
		if (checkEmail(the_email))
		{
			return post(the_email, the_attachment);
		}
		return EMAILSENTCODES.FAILUREEMAILADDRESS;
	}
	
	/**
	 * @param the_email
	 * @return
	 */
	private static EMAILSENTCODES post(String the_email, File the_attachment) {
		  try {   
             return new Delivery(buildMail(the_email, the_attachment)).deliver();   
          } catch (Exception e) {   
              Log.e(SENDINGERROR, e.getMessage(), e); 
          } 
		  return EMAILSENTCODES.FAILURESYSEM;

	}

	/**
	 * @param the_email
	 * @param the_attachment
	 * @return
	 */
	private static EmailLocal buildMail(String the_email, File the_attachment) {
		if (the_email != null)
		{
			return new EmailLocal(Sender.DUMMYHESARU, Sender.DUMMYBEEGA, the_email, EMAILSUBJECT, EMAILBODY, the_attachment);
		}
		return null;
	}

	/**
	 * 
	 */
	public static boolean checkEmail(String the_email) {
		  
		return android.util.Patterns.EMAIL_ADDRESS.matcher(the_email).matches();
	}

}
